package cn.dapeng.other;

/**
 * @BelongProject study
 * @BelongPackage cn.dapeng.other
 * @Description 阶乘的两个小例子
 * @ClassName FactorialTest
 * @Author xupenghong
 * @Date 2024/9/3 10:17
 * @Version 1.0.0
 */
public class FactorialTest {

    public static int m(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }

        int ans = 1, pre;
        pre = m_pre(n - 1);
        ans *= pre * n;

        return ans;
    }

    public static int mNum(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }

        int ans = 1, pre;

        for (int i = 2; i <= n; i++) {
            pre = m_pre(i);
            ans += pre;
        }
        return ans;
    }

    public static int m_pre(int n) {
        int ans = 1;
        for (int i = 2; i <= n; i++) {
            ans *= i;
        }
        return ans;
    }

    public static int f(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int ans = 1;
        for (int i = 2; i <= n; i++) {
            ans *= i;
        }
        return ans;
    }


    public static int fNum(int n) {
        if (n < 0) {
            return 0;
        }
        if (n == 1) {
            return 1;
        }
        int ans = 1, pre = 1;
        for (int i = 2; i <= n; i++) {
            pre *= i;
            ans += pre;
        }
        return ans;
    }


    public static void main(String[] args) {
        System.out.println(m(15));
        System.out.println(f(15));
        System.out.println();
        System.out.println(mNum(15));
        System.out.println(fNum(15));
    }
}
